package com.gandong8.trace.tool.config.mysql;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.gandong8.trace.tool.config.GlobalProperties;
import com.gandong8.trace.tool.config.mysql.properties.HikariCPProperties;
import com.gandong8.trace.tool.exception.GlobalException;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

//@Configuration
@EnableTransactionManagement
@EnableConfigurationProperties({HikariCPProperties.class, GlobalProperties.class})
@MapperScan(basePackages = "com.gandong8.trace.dao.mapper",sqlSessionFactoryRef = "hikariSqlSessionFactory")
@Slf4j
public class HikariCPConf {
    @Autowired
    HikariCPProperties myProps;

    @Autowired
    private GlobalProperties globalProperties;

    @Bean("hikariCPDataSource")
    public DataSource createDataSource(){
        if(!globalProperties.getHikariOpen()) {
            System.err.println("hikari datasource not allowed");
            return null;
        }
        if (myProps == null) {
            throw new GlobalException("HikariCPProperties is null");
        }
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setUsername(myProps.getUsername());
        dataSource.setPassword(myProps.getPassword());
        dataSource.setJdbcUrl(myProps.getUrl());
        dataSource.setDriverClassName(myProps.getDriverClassName());
        dataSource.setMaximumPoolSize(myProps.getMaxPoolSize());
        dataSource.setMinimumIdle(myProps.getMinIdle());
        log.info("-----hikari datasource start!-----");
        return dataSource;
    }

    @Bean("hikariSqlSessionFactory")
    @DependsOn({"hikariCPDataSource","mybatisConfiguration"})
    public SqlSessionFactory sqlSessionFactory(@Qualifier("hikariCPDataSource") DataSource dataSource,
                                               @Qualifier("mybatisConfiguration") MybatisConfiguration mybatisConfiguration
    ) throws Exception {
        if(!globalProperties.getHikariOpen()) {
            return null;
        }
        //SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        //factoryBean.setTypeAliasesPackage(this.getAliasesPackage());
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        //factoryBean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
        factoryBean.setConfiguration(mybatisConfiguration);
        return factoryBean.getObject();
    }



}
